#include<bits/stdc++.h>
using namespace std;
int check(string s)
{
	if (s.size() != 18)
		return 0;
	for (int i = 0; i < 18; i++)
		if (!isdigit(s[i]))
			return 0;
	return 1;
}
int main() {
    vector<vector<string> > res;
    vector<vector<string> > abnormal;
    int D, P;
    cin >> D >> P; // D天的数据， 每次需要相隔P天
    for (int d = 1; d <= D; d++) {
        int T, S;
        cin >> T >> S; // T条申请， 当天一共有S个口罩
        map<int, vector<string>> Map;
        for (int i = 1; i <= T; i++) {
            string name, idcard, status;
            int hh, mm, m;
            cin >> name >> idcard >> status;
            scanf("%d:%d", &hh, &mm);
            m = hh * 60 + mm;
            if (check(idcard)) {
                vector<string> vec;
                vec.push_back(name);
                vec.push_back(idcard);
                vec.push_back(status);
                vec.push_back(to_string(i))
                if (Map.find(m) != Map.end()) {
                    Map.insert(pair<int, vector<string> >(m + 1, vec));
                } else {
                    Map.insert(pair<int, vector<string> >(m, vec));
                }
                if (status == "1") abnormal.push_back(vec);
            }
        }
        if (Map.size() <= S) {
            for (auto iter = Map.begin(); iter != Map.end(); iter++)
                if (iter->second[3] + P + 1 >= d)
                    res.push_back(iter->second);
        } else {
            for (auto iter = Map.begin(); iter != Map.end(); iter++, S--) {
                if (S) {
                    if (iter->second[3] + P + 1 >= d)
                    res.push_back(iter->second);
                } else break;
            }
        }
    }
    for (auto & r : res) {
        cout << r[0] << r[1] << endl;
    }
    for (auto & a : abnormal) {
        cout << a[0] << a[1] << endl;
    }
    return 0;
}